{% extends "admin_base.html" %}
{% block title %}
健康新闻管理
{% endblock %}

{% block subtitle %}
健康资讯管理/新闻管理
{% endblock %}


{% block main %}健康新闻管理{% endblock %}

{% block content %}

<!-- 健康新闻主要就是管理员发布。而帖子是用户之间交流；不需要管理员发布 -->

<section class="section profile">
  <div class="row">

    <section class="section">
      <div class="row">



        <div class="col-xl-12">

          <div class="card">
            <div class="card-body">
              <div class="card-title">

                <div class="toolbar-btn-action">
                  <!-- <a class="btn btn-danger" href="#!"><i class="mdi mdi-window-close"></i> 批量删除</a> -->
                  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#basicModal">
                    发布新闻
                  </button>
                  <!-- 发布模态框 -->
                  <div class="modal fade" id="basicModal" tabindex="-1" aria-hidden="true" style="display: none;">
                    <div class="modal-dialog">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h5 class="modal-title">健康新闻</h5>
                          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">

                          <div class="col-xl-12">
                            <div class="card p-4">

                              <form action="{% url 'adminApp:health_news' %}" method="post" enctype="multipart/form-data">
                                <div class="row gy-4">
                                  {% csrf_token %}
                                  <input type="hidden" name="type" id="type" value="add">
                                  <input type="hidden" name="news_id" id="news_id" >

                                  <div class="col-md-12">
                                    <input type="text" id="news_title" name="news_title"  class="form-control"
                                      placeholder="标题" >
                                  </div>

                                  <div class="col-md-12">
                                    <textarea class="form-control" id="news_content" name="news_content" rows="6"
                                      placeholder="内容" ></textarea>
                                  </div>

                                  <div class="col-md-12">
                                    选择图片
                                    <img src="{{user.profile}}" alt="Profile" height="120" id="Profile"
                                      onclick="$(this).next().click();" onerror="this.src='/static/img/news-1.jpg' ">
                                    <input style="display: none;" type="file" name="profile" id="inprofile"
                                      onchange="inputFile(this)" accept="image/*">
                                    <input type="hidden" name="news_img" id="profile1">
                                    <script>
                                      function inputFile(obj) {
                                        const file = obj.files[0]
                                        var reader = new FileReader();
                                        reader.readAsDataURL(file)
                                        reader.onload = function () {
                                          $("#Profile").attr("src", this.result);
                                          $("#profile1").val($("#Profile").attr("src"));
                                        }
                                      }
                                    </script>

                                  </div>
                                  <button class="btn btn-info" type="submit"><i class="mdi mdi-window-close"></i>发布</button>
                                </div>

                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <!-- 1 -->
            <div class="card-body">

              <div class="table-responsive">
                <table class="table table-bordered">
                  <thead>
                    <tr>
                      <!-- <th>
                        <label class="lyear-checkbox checkbox-primary">
                          <input type="checkbox" id="check-all"><span></span>
                        </label>
                      </th> -->
                      <th>新闻编号</th>
                      <th>标题</th>
                      <th>图片</th>
                      <th>发布日期</th>

                      <th>操作</th>
                    </tr>
                  </thead>
                  <tbody>
                    {% for i in news %}
                    <tr>
                      <!-- <td>
                        <label class="lyear-checkbox checkbox-primary">
                          <input type="checkbox" name="ids[]" value="1"><span></span>
                        </label>
                      </td> -->
                      <td>{{i.news_id}}</td>
                      <td>{{i.news_title}}</td>
                      <td><img src="{{i.news_img}}" alt="" width="40px" id="img{{ i.news_id }}"></td>
                      <td class="hidden" style="display: none" id="news_content{{ i.news_id }}">{{ i.news_content }} </td>
                      <td>{{i.c_time}}</td>
                      <td>
                        <div class="btn-group">
                          <a type="button" class="btn btn-outline-info btn-sm"
                            href="{% url 'adminApp:health_news' %}?id={{i.news_id}}&t=show">详情</a>
                          <span>|</span>
                          <button type="button" class="btn btn-outline-warning btn-sm"
                            onclick='change_data("{{i.news_title}}","{{i.news_img}}",$("#news_content{{ i.news_id }}").text(),"{{i.news_id}}")'>修改</button>
                          <span>|</span>
                          <a type="button" class="btn btn-outline-danger btn-sm"
                            href="{% url 'adminApp:health_news' %}?id={{i.news_id}}&t=delete">删除</a>
                        </div>
                      </td>
                    </tr>
                    {% endfor %}

                    
                    <script>
                      function change_data(title,img,content,news_id) {
                        $('#basicModal').modal('show')
                        
                        $('#type').val('change')
                        $('#news_title').val(title)
                        $('#Profile').attr("src", img);
                        $("#profile1").val(img);
                        $('#news_content').val(content)
                        $('#news_id').val(news_id)
                        



                        // $('#identifier').modal('hide')
                        // $('#type').val('add')
                      }

                    </script>


                  </tbody>
                </table>
              </div>
              <!-- 分页 -->
              <!-- <ul class="pagination">
                <li class="disabled"><span>«</span></li>
                <li class="active"><span>1</span></li>
                <li><a href="#1">2</a></li>
                <li><a href="#1">3</a></li>
                <li><a href="#1">4</a></li>
                <li><a href="#1">5</a></li>
                <li><a href="#1">6</a></li>
                <li><a href="#1">7</a></li>
                <li><a href="#1">8</a></li>
                <li class="disabled"><span>...</span></li>
                <li><a href="#!">14452</a></li>
                <li><a href="#!">14453</a></li>
                <li><a href="#!">»</a></li>
              </ul> -->

            </div>


          </div>
        </div><!-- End Bordered Tabs -->

      </div>
  </div>
</section>
{% endblock %}